<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear v-model="queryParam.dwh" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="所属专业">
                <a-select allowClear v-model="queryParam.zdzy" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="导师类别">
                <a-select mode="multiple" allowClear v-model="queryParam.dslbm">
                  <a-select-option v-for="i in TYPE0360" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="导师状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPE0320" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="是否在岗">
                <a-select allowClear v-model="queryParam.dssfzg">
                  <a-select-option v-for="i in TYPE0321" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="招生年度">
                <a-select allowClear v-model="queryParam.zsnd">
                  <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <!--<a-button type="primary" @click="showContract">发放聘书</a-button>-->
              <a-button type="primary" @click="batchDelete">批量删除</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
<!--        <a-row style="margin-bottom: 20px;">
          <a-upload style="display:inline-block" name="file" :multiple="true" action="https://www.mocky.io/v2/5cc8019d300000980a055e76" @change="handleChange">
            <a-button>
              <a-icon type="upload" /> 点击选择导入文件
            </a-button>
          </a-upload>
          <a-button type="primary">导入</a-button>
        </a-row>-->
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{ x: 2500 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="psffrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a>
        <template slot="operation" slot-scope="a,b">
          <a @click="toEdit(b)">导师信息</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="toRecuit(b)">招生信息</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="showRecruit(b)">招生单位</a>
              </a-menu-item>
              <!-- <a-menu-item>
                <a @click="showBreak(b)">破格处理</a>
              </a-menu-item> -->
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>

    <recruit-unit :visible="recruitVisible" :teacherInfo="teacherInfo" @close="recruitVisible = false"/>
    <break-rule :visible="breakVisible" :teacherInfo="teacherInfo" @close="breakVisible = false"/>
    <contract
      :visible="contractVisible"
      @close="contractVisible = false"
      @issue="issueContract"/>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import recruitUnit from './components/recruitUnit'
import breakRule from './components/breakRule'
import contract from './components/contract'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '导师编号',
          dataIndex: 'dsbh',
          scopedSlots: { customRender: 'dsbh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '身份证号',
          dataIndex: 'sfzjh'
        },
        {
          title: '招生年度',
          dataIndex: 'zsnd'
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '指导专业',
          dataIndex: 'zdzymc',
          width:180
        },
        {
          title: '导师类别',
          dataIndex: 'dslbmc'
        },
        {
          title: '研究方向',
          dataIndex: 'yjfxmc',
          width: '220px'
        },
        {
          title: '招生单位',
          dataIndex: 'zsdwmc',
          width: '220px'
        },
        {
          title: '招生层次',
          dataIndex: 'zsztmc'
        },
        {
          title: '导师职称',
          dataIndex: 'zcmc'
        },
        {
          title: '导师状态',
          dataIndex: 'shztmc'
        },
        {
          title: '聘书发放日期',
          dataIndex: 'psffrq',
          scopedSlots: { customRender: 'psffrq' }
        },
        {
          title: '是否在岗',
          dataIndex: 'dssfzgmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: '160px',
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        this.$refs.table && this.$refs.table.clearSelected()
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.teacher.teacherList(params).then(res => {
          console.log(res.data)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {},
      recruitVisible: false,
      breakVisible: false,
      contractVisible: false,
      teacherInfo: {}
    }
  },
  components: {STable, recruitUnit, breakRule, contract},
  computed: {
    ...mapState({
      TYPE0360: state => state.app['TYPE0360'], // 导师类别
      TYPE0320: state => state.app['TYPE0320'], // 导师状态
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      TYPE0387: state => state.app['TYPE0387'] // 招生学年
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0360')
    this.$store.dispatch('app/setDictionary', 'TYPE0320')
    this.$store.dispatch('app/setDictionary', 'TYPE0321')
    this.$store.dispatch('app/setDictionary', 'TYPE0387')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zdzy', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    direct (data) {
      this.$router.push(`/teacher/info/${data.dsbh}`)
    },
    toEdit (data) {
      this.$router.push(`/teacher/info/${data.dsbh}/edit`)
    },
    toRecuit (data) {
      this.$router.push(`/teacher/info/${data.dsbh}/recruit`)
    },
    showRecruit (data) {
      this.recruitVisible = true
      this.teacherInfo = data
    },
    showBreak (data) {
      this.breakVisible = true
      this.teacherInfo = data
    },
    showContract (data) {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要发放聘书的导师')
        return
      }
      this.contractVisible = true
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的导师')
        return
      }
      this.$confirm({
        title: '确定删除所选中的导师信息?',
        content: '当您点击确定按钮后，这些导师信息将会被彻底删除',
        centered: true,
        onOk: () => {
          let teacherIds = []
          this.selectedRows.forEach(teacher => {
            teacherIds.push(teacher.id)
          })
          this.$api.teacher.deleteTeachers(teacherIds.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    issueContract (param) {
      let dsbh = this.selectedRows.map(t => t.dsbh)
      let params = Object.assign({dsbh}, param)
      this.$DateToString(params)
      this.$api.teacher.issueContract(params).then(res => {
        this.contractVisible = false
        this.$message.success(res.msg)
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
      })
    },
    handleChange (info) {
      console.log(info)
      // if (info.file.status !== 'uploading') {
      //   console.log(info.file, info.fileList);
      // }
      // if (info.file.status === 'done') {
      //   this.$message.success(`${info.file.name} file uploaded successfully`);
      // } else if (info.file.status === 'error') {
      //   this.$message.error(`${info.file.name} file upload failed.`);
      // }
    }
  }
}
</script>

<style>

</style>
